<?php

namespace App\Http\Controllers\Wechat\Shop;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

use App\Http\Controllers\Controller;
class UploadController extends Controller
{
    //
     /**
     * 接收上传文件
     * @param Request $request
     * @return array
     */
    public function upload(Request $request )
    {
        $rs['status'] = false;
        if ($request->isMethod('post')) {

            $file = $request->file('pic');

            // 文件是否上传成功
            if ($file->isValid()) {
                $savepath = '/public/product/'.date('Ymd').'/';
                $filename = uploadFile($file,$savepath);               
                $rs['status'] = true;
                $rs['data'] = '/storage/product/'.date('Ymd').'/'.$filename;
                return response()->json($rs);

            }

        }
        return response()->json(['rs' => false, 'msg' => '不是正确请求方式']);
        
    }

    public function uploadBase64(Request $request)
    {
         $rs['status'] = true;
         try {
            $data       = $request->all();
            $imgdata    = $data['pic'];
            $base64_str = substr($imgdata, strpos($imgdata, ",")+1);
            $image      = base64_decode($base64_str);
            $imgname    = 'public/task/'.date('YmdHis').rand(100,999).'.png';
            //Storage::disk('public')->put($imgname,$image); 
            $flag = Storage::put($imgname, $image, 'public'); 
            
            //Storage::putFile('task', $image); 
         } catch (\Exception $e) {
            $rs['status'] = false;
            $rs['msg'] = $e->getMessage();
         }
         $rs['data'] = 'storage/'.$imgname;
         return  response()->json($rs);
    }

    


       
}
